/**
* @author Ashish Gupta
* CVSSP, University of Surrey
* Copyright (c) 2010 Ashish Gupta
* All rights reserved.
*/
/**
 * 
 */
package experiment;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import projFn.DataSet;
import projFn.ImageSet;
import projFn.Pop;

import configuration.GenePoolConfiguration;
import configuration.VisualRecognitionConfiguration;

/**
 * @author Ashish
 *
 */
public class runProjFnGA {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		VisualRecognitionConfiguration vrConfig = null;
		try {
			vrConfig = new VisualRecognitionConfiguration(args[0]);
		} catch (IOException e1) {
			System.out.println("Error in processing VisualRecognitionConfiguration File.");
			e1.printStackTrace();
		}
		
		GenePoolConfiguration genePoolConfig = null;
		try {
			genePoolConfig = new GenePoolConfiguration(args[1]);
		} catch (IOException e) {
			System.out.println("Error in processing GeneConfiguration File.");
			e.printStackTrace();
		}
		
		ImageSet trainImageSet = new ImageSet();
//		ImageSet valImageSet = new ImageSet();
		
		trainImageSet.read(vrConfig.trainimageSetURI);
//		valImageSet.read(vrConfig.valimageSetURI);
		
		DataSet dataSet = new DataSet(trainImageSet,vrConfig.hashCodeFolderName,genePoolConfig.getNumGenes());
		Pop pop = new Pop();
		pop.spawnPop(genePoolConfig.getNumChromosomes(), Integer.parseInt(vrConfig.maxHashCode), genePoolConfig.getNumGenes());
		
		File fitnessProfileFile = new File(vrConfig.fitnessProfileURL);
		FileWriter fitness_fw = new FileWriter(fitnessProfileFile);
		BufferedWriter fitness_bw = new BufferedWriter(fitness_fw);
		
		for(int i = 0; i < genePoolConfig.getNumGenerations();i++){
			pop.evolvePop(trainImageSet, dataSet, genePoolConfig.getNumGenes());
			double fitness = pop.computeMaxFitness();
			System.out.println(i + " : " + fitness);
			fitness_bw.write(i + " : " + fitness);
		}

		fitness_bw.close();
		fitness_fw.close();
	}

}
